package com.mvc.controller;

import com.alibaba.fastjson.JSONObject;
import com.mvc.entity.Sku;
import com.mvc.entity.Spu;
import com.mvc.model.JsonUtil;
import com.mvc.model.Result;
import com.mvc.service.ProductService;
import io.swagger.models.auth.In;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @ProjectName: dior
 * @Package: com.mvc.controller
 * @ClassName: ProductController
 * @Description: TODO
 * @Author: Bang
 * @Date: 2021/12/1 17:18
 * @Version: 1.0
 **/
@Controller
@RequestMapping("/product")
public class ProductController {
    @Resource
    ProductService productService;

    @RequestMapping(value = "/query.do", produces = "text/html;charset=UTF-8")
    @ResponseBody
    public String query(@RequestParam(required = false) Integer id,
                        @RequestParam(required = false) String name,
                        @RequestParam(required = false) Integer leibie,
                        @RequestParam(required = false) Integer page,
                        @RequestParam(required = false) Integer limit){
        //参数列表
        Map<String,Object> pars=new HashMap<>();
        pars.put("id",id);
        pars.put("name",name);
        pars.put("leibie",leibie);
        if (page!=null && limit !=null){
            pars.put("offset",(page-1)*limit);
            pars.put("limit",limit);
        }
        List<Spu> spuList = productService.query(pars);
        int count = productService.count(pars);
        Result result = new Result();
        result.setCode(0);
        result.setCount(count);
        result.setData(spuList);
        return JsonUtil.stringify(result);
    }

    @RequestMapping(value = "/updateStatus.do", produces = "text/html;charset=UTF-8")
    @ResponseBody
    public String updateStatus(@RequestParam(required = false) Integer id,
                               @RequestParam(required = false) Integer status){
        Integer n = productService.updateStatus(id, status);
        Result result = new Result();
        if(n == 1) {
            result.setCode(0);
            result.setMsg("成功");
        }else {
            result.setCode(1000);
            result.setErrorMsg("失败");
        }
        return JsonUtil.stringify(result);
    }

    @RequestMapping(value = "/updateSpu.do", produces = "text/html;charset=UTF-8")
    @ResponseBody
    public String update(@RequestBody  String json){
        Spu spu = JSONObject.parseObject(json,Spu.class);
        Integer n = productService.update(spu);
        Result result = new Result();
        if(n == 1) {
            result.setCode(0);
            result.setMsg("成功");
        }else {
            result.setCode(1000);
            result.setErrorMsg("失败");
        }
        return JsonUtil.stringify(result);
    }

    @RequestMapping(value = "/updateSku.do", produces = "text/html;charset=UTF-8")
    @ResponseBody
    public String updateSku(@RequestBody  String json){
        Sku sku = JSONObject.parseObject(json,Sku.class);
        Integer n = productService.updateSku(sku);
        Result result = new Result();
        if(n == 1) {
            result.setCode(0);
            result.setMsg("成功");
        }else {
            result.setCode(1000);
            result.setErrorMsg("失败");
        }
        return JsonUtil.stringify(result);
    }

    @RequestMapping(value = "/delSpu.do", produces = "text/html;charset=UTF-8")
    @ResponseBody
    public String delSpu(@RequestParam Integer id){
        Integer n = productService.delSpu(id);
        Result result = new Result();
        if(n == 1) {
            result.setCode(0);
            result.setMsg("成功");
        }else {
            result.setCode(1000);
            result.setErrorMsg("失败");
        }
        return JsonUtil.stringify(result);
    }

    @RequestMapping(value = "/delSku.do", produces = "text/html;charset=UTF-8")
    @ResponseBody
    public String delSku(@RequestParam Integer id){
        Integer n = productService.delSku(id);
        Result result = new Result();
        if(n == 1) {
            result.setCode(0);
            result.setMsg("成功");
        }else {
            result.setCode(1000);
            result.setErrorMsg("失败");
        }
        return JsonUtil.stringify(result);
    }

    @RequestMapping(value = "/add.do", produces = "text/html;charset=UTF-8")
    @ResponseBody
    public String add(@RequestBody  String json){
        Spu spu = JSONObject.parseObject(json,Spu.class);
        Integer n = productService.add(spu);
        Result result = new Result();
        if(n == 1) {
            result.setCode(0);
            result.setMsg("成功");
        }else {
            result.setCode(1000);
            result.setErrorMsg("失败");
        }
        return JsonUtil.stringify(result);
    }

    @RequestMapping(value = "/all.do", produces = "text/html;charset=UTF-8")
    @ResponseBody
    public String all(@RequestParam Integer typeId,
                        @RequestParam(required = false) String value){
        //参数列表
        Map<String,Object> pars=new HashMap<>();
        pars.put("typeId",typeId);
        pars.put("value",value);
        List<Spu> spuList = productService.all(pars);
        Result result = new Result();
        result.setCode(0);
        result.setData(spuList);
        return JsonUtil.stringify(result);
    }
}
